<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="ruleForm" :model="form" :rules="rules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-col :span="24">
              <a-form-model-item label="素材图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="imgUrls">
                <j-image-upload :isMultiple="true" :num="9" text="上传" v-model="form.imgUrls"></j-image-upload>
                <span>(至多9张,推荐尺寸：750*750像素；小于3M；支持png/jpeg/jpg)</span>
              </a-form-model-item>
            </a-col>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="选择商品" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="itemId">
              <div>{{form.title}}</div>
              <a-button @click="goodSelect">选择商品</a-button>
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="是否显示" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isShow">
              <j-dict-select-tag type="radio" v-model="form.isShow"  dictCode="yn" placeholder="请选择是否显示" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="素材文本" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="richText">
              <a-textarea v-model="form.richText" rows="4" placeholder="请输入素材文本" />
            </a-form-model-item>
          </a-col>

        </a-row>
      </a-form-model>
    </j-form-container>
    <gms-good-ref ref="modalGoods" @ok="modalGoodsOk" />
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import JFormContainer from '@/components/jeecg/JFormContainer'
import JDate from '@/components/jeecg/JDate'
import JDictSelectTag from "@/components/dict/JDictSelectTag"
import JImageUpload from '@/components/jeecg/JImageUpload'
import GmsGoodRef from '@/views/gospel/mallBase/good/modules/GmsGoodRef'


export default {
  name: 'GmsGoodMaterialForm',
  components: {
    JFormContainer,
    JDate,
    JDictSelectTag,
    JImageUpload,
    GmsGoodRef
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => { },
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data () {
    return {
      form: {
        itemId: '',
        title: '',
        itemType: 0,
        uid: '',
        shopId: '',
        type: 1,
        richText: '',
        isShow: 1,
        shareNum: '',
        isOfficial: 0,
        auditStatus: 2,
        reason: '',
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      rules: {
        imgUrls: [
          { required: true, message: '请选择商品!' },
        ],
        itemId: [
          { required: true, message: '请输入商品编号!' },
        ],
        richText: [
          { required: true, message: '请输入素材内容富文本!' },
        ],
      },
      url: {
        add: "/mallPlus/gmsGoodMaterial/add",
        edit: "/mallPlus/gmsGoodMaterial/edit",
        queryById: "/mallPlus/gmsGoodMaterial/queryById"
      }
    }
  },
  computed: {
    formDisabled () {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton () {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created () {

  },
  methods: {
    add (type) {
      this.edit({
        itemId: '',
        title: '',
        itemType: 0,
        uid: '',
        shopId: '',
        type: type,
        richText: '',
        isShow: 1,
        shareNum: '',
        isOfficial: 0,
        auditStatus: 2,
        reason: '',
      })
    },
    edit (record) {
      this.form = Object.assign({}, record)
      this.visible = true
    },
    goodSelect () {
      let self = this
      this.$refs.modalGoods.visible = true
      this.$refs.modalGoods.open()
    },
    modalGoodsOk (row) {
      const self = this
      this.form.itemId = row.id
      this.form.title = row.title
    },
    submitForm () {
      const that = this
      // 触发表单验证
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.form.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign({}, this.form)
          console.log("表单提交数据", formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    },
    popupCallback (row) {
      this.form = row
    },
  }
}
</script>
